package cn.com.headfree.datapermits.service.impl;

import java.util.Calendar;
import java.util.List;
import java.util.Optional;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gta.edu.sdk.base.service.impl.BaseServiceImpl;
import com.gta.edu.sdk.common.dto.Page;
import com.gta.edu.sdk.util.IdGen;

import cn.com.headfree.datapermits.dao.IDataPermitsDao;
import cn.com.headfree.datapermits.service.IDataPermitsService;
import cn.com.headfree.entity.TDataRolePermission;
import cn.com.headfree.entity.TMouldInfo;
import cn.com.headfree.entity.isc.User;

/**  
 * @Title  DataPermitsServiceImpl.java
 * @Package cn.com.headfree.datapermits.service.impl   
 * @Description 数据权限信息serviceImpl
 * @author yangwang 
 * @date  2020年7月3日 下午3:46:31   
 */
@Service("dataPermitsService")
public class DataPermitsServiceImpl extends BaseServiceImpl<TDataRolePermission> implements IDataPermitsService {
	private static final Logger LOG = Logger.getLogger(DataPermitsServiceImpl.class);
	
	@Autowired
	private IDataPermitsDao dataPermitsDao;

	/**
	 * 保存记录
	 * @param user
	 * @param permits
	 *  */
	@Transactional
	@Override
	public void save(User user,TDataRolePermission permits) {
		if((null != permits)&&(StringUtils.isNotBlank(permits.getRoleCode()))) {
			Calendar cal = Calendar.getInstance();
			if(StringUtils.isNotBlank(permits.getId())) {//更新
				permits.setUpdateTime(cal.getTime());
				if((null != user)&&(StringUtils.isNotBlank(user.getId()))) {
					permits.setUpdaterId(user.getFax());
					permits.setUpdaterName(user.getName());
				}
				dataPermitsDao.edit(permits);
			}else {//新增
				permits.setId(IdGen.uuid());
				permits.setCreateTime(cal.getTime());
				permits.setUpdateTime(cal.getTime());
				if((null != user)&&(StringUtils.isNotBlank(user.getId()))) {
					permits.setCreaterId(user.getId());
					permits.setCreaterName(user.getName());
					permits.setUpdaterId(user.getId());
					permits.setUpdaterName(user.getName());
				}
				dataPermitsDao.insert(permits);
			}
		}
	}
	
	/**
	 * 删除记录
	 * @param user
	 * @param ids
	 *  */
	@Transactional
	@Override
	public void delete(User user,String ids) {
		if(StringUtils.isNotBlank(ids)) {
			dataPermitsDao.delete(user,ids);
		}
	}
	
	/**
	 * 获取列表记录
	 * @param permits
	 *  */
	@Override
	public List<TDataRolePermission> getList(TDataRolePermission permits){
		List<TDataRolePermission> roleList = dataPermitsDao.getList(permits);
		if((null != roleList)&&(0 < roleList.size())){
			List<TMouldInfo> mouldList = dataPermitsDao.getMouldInfos(null);
			if((null != mouldList)&&(0 < mouldList.size())) {
				for(TDataRolePermission r:roleList) {
					if((null != r)&&(StringUtils.isNotBlank(r.getId()))&&(StringUtils.isNotBlank(r.getBusinessTypeId()))) {
						Optional<TMouldInfo> opt = mouldList.stream().filter(d -> ((null !=d)&&(StringUtils.isNotBlank(d.getId()))&&(StringUtils.equals(d.getId(), r.getBusinessTypeId())))).findFirst();
						if(opt.isPresent()) {
							r.setBusinessTypeName(opt.get().getMouldName());
						}
					}
				}
			}
		}else {
			roleList = null;
		}
		return roleList;
	}
	
	/**
	 * 获取分页记录
	 * @param permits
	 *  */
	@Override
	public void getPage(Page<TDataRolePermission> page,TDataRolePermission permits) {
		dataPermitsDao.getPage(page, permits);
		if((null != page)&&(null != page.getResult())&&(0 < page.getResult().size())){
			List<TMouldInfo> mouldList = dataPermitsDao.getMouldInfos(null);
			if((null != mouldList)&&(0 < mouldList.size())) {
				for(TDataRolePermission r:page.getResult()) {
					if((null != r)&&(StringUtils.isNotBlank(r.getId()))&&(StringUtils.isNotBlank(r.getBusinessTypeId()))) {
						Optional<TMouldInfo> opt = mouldList.stream().filter(d -> ((null !=d)&&(StringUtils.isNotBlank(d.getId()))&&(StringUtils.equals(d.getId(), r.getBusinessTypeId())))).findFirst();
						if(opt.isPresent()) {
							r.setBusinessTypeName(opt.get().getMouldName());
						}
					}
				}
			}
		}
	}
	
}
